package com.shelpe.services.tenant.service.impl;

import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.shelpe.services.tenant.model.Category;
import com.shelpe.services.tenant.model.Products;
import com.shelpe.services.tenant.mongomodel.ForbiddenCategory;
import com.shelpe.services.tenant.mongomodel.ForbiddenWord;
import com.shelpe.services.tenant.service.CategoryService;
import com.shelpe.services.tenant.service.ViolationDetectionService;

/*
 * 
 * 违规检测，目前包括
 * 1、违规关键词检测
 * 2、违规类目检测
 * */

@Service
public class ViolationDetectionServiceImpl implements ViolationDetectionService{
	
	private static final Logger logger = LoggerFactory.getLogger(ViolationDetectionServiceImpl.class);
	
	@Autowired
	private CategoryService categoryService;
	
	@Override
	public JSONArray forbiddenWordDetect(Products product, Iterable<ForbiddenWord> items) {
		// TODO Auto-generated method stub
		JSONArray  forbiddenWord = new JSONArray();
		
		
		if(items != null){
			Iterator<ForbiddenWord> it = items.iterator();
			
			while(it.hasNext()){
				ForbiddenWord forWord = it.next();
				String title = product.getTitle();
				
				if(title.contains(forWord.getWord())){
					
					forbiddenWord.add(forWord.getWord());
				}
			}
		}
		
		return forbiddenWord;
	}


	@Override
	public JSONArray forbiddenCategoryDetect(Products product, Iterable<ForbiddenCategory> items) {
		// TODO Auto-generated method stub
		JSONArray  forbiddenCat = new JSONArray();
		
		if(items != null){
			Long userId = product.getUserId();
			Integer type = 1;
			Integer cid = product.getCid();
			String categoryIds = cid.toString();
			
			Category cat = categoryService.getCategoryById(cid);//优先从数据库中获取
			
			if(cat == null){
				List<Category> cats = categoryService.getCategoryFromProxy(userId, type, categoryIds);
				
				if(cats != null && cats.size() > 0){
					cat = cats.iterator().next();
					categoryService.insertCategory(cat);//保存至数据库
				}
			}
			
			if(cat != null){
				
				String pathName = cat.getCatPathName();
				//logger.info("forbiddenCategoryDetect path： {}", pathName);
				Iterator<ForbiddenCategory> it = items.iterator();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
				String productProps = product.getPropsName();
				while(it.hasNext()){
					ForbiddenCategory category = it.next();
					String proName = category.getPropsName();
					if(pathName.contains(category.getCategory()) 
							&& (proName == null || (productProps != null && productProps.contains(proName)))){
						
						forbiddenCat.add(category.getCategory());
					}
				}
			}else{
				logger.info("forbiddenCategoryDetect can not find category info, cid: {}", cid);
			}
		}

		return forbiddenCat;
	}
	

}
